//
//  HZ_jibuqiCollectionViewCell.m
//  HZ_Beautiful_Music
//
//  Created by dllo on 16/4/13.
//  Copyright © 2016年 ui. All rights reserved.
//

#import "HZ_jibuqiCollectionViewCell.h"
#import <CoreMotion/CoreMotion.h>

@interface HZ_jibuqiCollectionViewCell ()

@property (nonatomic, strong) UILabel *label;
@property (nonatomic, strong) UILabel *labelbu;
@property (nonatomic, strong) UIButton *morebutton;
@property (nonatomic, strong) CMPedometer *pedonmeter;
@property (nonatomic, strong) UIImageView *image;
@property (nonatomic, assign) BOOL iskaishi;
@end
@implementation HZ_jibuqiCollectionViewCell
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    
    if ( self) {
        
        self.backgroundColor = [UIColor orangeColor];
      
        self.image = [[UIImageView alloc] initWithFrame:self.bounds];
        
        self.image.image = [UIImage imageNamed:@"14-160329151I4406.jpg"];
        _image.userInteractionEnabled = YES;
        [self addSubview:_image];
        
        self.label = [[UILabel alloc] initWithFrame:CGRectMake(self.bounds.size.width / 2, 50, self.bounds.size.width / 5, self.bounds.size.width / 5)];
        self.label.text = @"0 ";
        
        [_label.layer setCornerRadius:_label.bounds.size.width / 2];
//        _label.textAlignment = NSTextAlignmentCenter;
        _label.layer.masksToBounds = YES;
        _label.backgroundColor = [UIColor colorWithRed:0.690 green:0.855  blue:1.000 alpha:1.000];
        _label.alpha = 0.5;
        [self addSubview:_label];
        
        self.labelbu = [[UILabel alloc] initWithFrame:CGRectMake(self.bounds.size.width / 2 + self.bounds.size.width / 10, 50 + self.bounds.size.width / 20, self.bounds.size.width / 10, self.bounds.size.width / 10)];
        self.labelbu.text = @"步";
        _labelbu.alpha = 0.5;
        _labelbu.textAlignment = NSTextAlignmentCenter;
        _labelbu.backgroundColor = [UIColor clearColor];
        [self addSubview:_labelbu];
        
        _iskaishi = YES;
        self.morebutton = [UIButton buttonWithType:UIButtonTypeSystem];
//        _morebutton.backgroundColor = [UIColor whiteColor];
        _morebutton.frame = CGRectMake( self.bounds.size.width / 2 , 130, self.bounds.size.width / 5, self.bounds.size.width / 8);
        [self.morebutton setTitle:@"开 始" forState:UIControlStateNormal];
        [_morebutton addTarget:self action:@selector(morebutton:) forControlEvents:UIControlEventTouchUpInside];
        
        [_image addSubview:_morebutton];
    }
 
    return self;
  
}

-(void)morebutton:(UIButton *)morebutton{
    if (self.iskaishi) {
        self.iskaishi = NO;
        if([CMPedometer isStepCountingAvailable])
        {
            self.label.text = @"0 ";
            
            NSDate *localDate = [NSDate date];
            [self.pedonmeter startPedometerUpdatesFromDate:localDate withHandler:^(CMPedometerData * _Nullable pedometerData, NSError * _Nullable error) {
                self.label.text = [NSString stringWithFormat:@"%@" , pedometerData.numberOfSteps];
                [self.morebutton setTitle:@"停 止" forState:UIControlStateNormal];
                _pedonmeter = [[CMPedometer alloc] init];
            }];
        } else {
            _iskaishi = YES;
            [self.morebutton setTitle:@"开 始" forState:UIControlStateNormal];

            NSLog(@"不可用");
            NSNotificationCenter *hsmorebutton = [NSNotificationCenter defaultCenter];
            [hsmorebutton postNotificationName:@"hsmorebutton" object:nil userInfo:nil];
        }
    }
}
-(void)layoutSubviews{
    [super layoutSubviews];

    _image.frame = self.bounds;

}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
